package lock.lock;

public class TestMyLock {

    private int value;
    private MyLock myLock = new MyLock();

    public int getValue() {
        myLock.lock();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        value ++;
        myLock.unlock();
        return value;
    }

    public void a() {
        myLock.lock();
        System.out.println("a");
        b();
        myLock.unlock();
    }

    public void b() {
        myLock.lock();
        System.out.println("b");
        myLock.unlock();
    }

    public static void main(String[] args) {
        TestMyLock testMyLock = new TestMyLock();

        //检测是否会并发
        Runnable runnable = () -> {
          for (int i = 0; i<100; i++) {
              System.out.println(Thread.currentThread().getId()+",值：" + testMyLock.getValue());
          }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
        
        //检测重入锁
//        Runnable runnable1 = () -> testMyLock.a();
//        new Thread(runnable1).start();
    }
}